import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

const __filename = path.dirname(import.meta.url)
const __dirname = fileURLToPath(__filename)

// 写日志
function writeLog(writeStream, log) {
	writeStream.write(log + '\n')
}

// 生成 write stream
function createWriteStream(fileName) {
	const fullFileName = path.resolve(__dirname, `../../logs/${fileName}`)
	const writeStream = fs.createWriteStream(fullFileName, { flags: 'a' })

	return writeStream
}

const accessWriteStream = createWriteStream('access.log')

// 写访问日志
function access(log) {
	writeLog(accessWriteStream, log)
}

export { access }
